package com.neuedu.servlet;

import com.neuedu.pojo.Admin;
import com.neuedu.service.ILoginService;
import com.neuedu.service.LoginServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
    private static final ILoginService loginService = new LoginServiceImpl();
    private static final Admin admin = new Admin();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //resp.sendRedirect();重定向
        //req.getRequestDispatcher();转发
        admin.setUsername(req.getParameter("username"));
        admin.setPassword(req.getParameter("password"));
        if (loginService.login(admin).getUsername() != null) {
            HttpSession session = req.getSession();
            session.setAttribute("username", admin.getUsername());
            session.setAttribute("password", admin.getPassword());
            resp.sendRedirect("ok.jsp");
        } else {
            resp.sendRedirect("error.html");
        }
        System.out.println("protected void doGet");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("protected void doPost");

    }
}
